package com.dwxt.sport.business.service;

import com.dwxt.common.base.StatusCode;
import com.dwxt.common.base.YesOrNo;
import com.dwxt.common.constant.VariableConstants;
import com.dwxt.common.util.SnowIdUtil;
import com.dwxt.sport.business.dao.*;
import com.dwxt.sport.business.entity.IndexDeviceInfo;
import com.dwxt.sport.entity.WifeDevice;
import com.dwxt.sport.exception.CustomException;
import com.dwxt.sport.service.WifeDeviceService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 首页-设备统计 Service
 *
 * @author Kuang.JiaZhuo
 * @date 2020-08-11 15:58
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class IndexDeviceInfoService {
    private final WifeDeviceService wifeDeviceService;

    private final IndexDeviceInfoMapper indexDeviceInfoMapper;
    private final OutdoorFitnessEquipmentMapper outdoorFitnessEquipmentMapper;
    private final FitnessSurveyEquipmentMapper fitnessSurveyEquipmentMapper;
    private final SmartDeviceEquipmentMapper smartDeviceEquipmentMapper;
    private final AdolescentSportsHealthEquipmentMapper adolescentSportsHealthEquipmentMapper;
    private final QuinquagenarianSportsHealthEquipmentMapper quinquagenarianSportsHealthEquipmentMapper;
    private final DeformitySportsHealthEquipmentMapper deformitySportsHealthEquipmentMapper;
    private final SportsTrainingDetailsMapper sportsTrainingDetailsMapper;
    private final WisdomTrailsEquipmentMapper wisdomTrailsEquipmentMapper;
    private final WisdomVenuesEquipmentMapper wisdomVenuesEquipmentMapper;

    /**
     * @方法名：insertOrUpdateStreetDeviceClassify
     * @描述： 新增或修改【首页-设备统计】
     * @作者： kjz
     * @日期： Created in 2020/8/11 17:20
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertOrUpdateStreetDeviceClassify(IndexDeviceInfo indexDeviceInfo) {
        String id = indexDeviceInfo.getId();
        if (null == id || id.trim().length() > 0) {
            //新增
            indexDeviceInfo.setCreateTime(LocalDateTime.now())
                    .setIsDelete(VariableConstants.STRING_CONSTANT_0)
                    .setId(SnowIdUtil.getInstance().nextId());
            return indexDeviceInfoMapper.insert(indexDeviceInfo);
        } else {
            //更新
            indexDeviceInfo.setUpdateTime(LocalDateTime.now());
            return indexDeviceInfoMapper.updateByPrimaryKeySelective(indexDeviceInfo);
        }
    }

    /**
     * @方法名：selectIndexDeviceInfoList
     * @描述： 获取【首页-设备统计】数据
     * @作者： kjz
     * @日期： Created in 2020/8/11 17:45
     */
    public List<IndexDeviceInfo> selectIndexDeviceInfoList() {
        List<IndexDeviceInfo> indexDeviceInfos = indexDeviceInfoMapper.selectIndexDeviceInfoList();
        indexDeviceInfos.forEach(indexDeviceInfo -> {
            String classification = indexDeviceInfo.getClassification();
            Integer equipmentNumber = 0;
            /** 假数据，原型没有体现是否启用的概念，所以此处将设备状态为正常数量的定义为启用数量*/
            Integer enableAmount = 0;
            //室外健身器材
            if(VariableConstants.STRING_CONSTANT_2.equals(classification)){
                equipmentNumber = outdoorFitnessEquipmentMapper.selectCountAll();
                enableAmount = outdoorFitnessEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //体质监测中心
            if(VariableConstants.STRING_CONSTANT_4.equals(classification)){
                equipmentNumber = fitnessSurveyEquipmentMapper.selectCountAll();
                enableAmount = fitnessSurveyEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //智能装备
            if(VariableConstants.STRING_CONSTANT_5.equals(classification)){
                equipmentNumber = smartDeviceEquipmentMapper.selectCountAll();
                enableAmount = smartDeviceEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //青少年运动健康
            if(VariableConstants.STRING_CONSTANT_6.equals(classification)){
                equipmentNumber = adolescentSportsHealthEquipmentMapper.selectCountAll();
                enableAmount = adolescentSportsHealthEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //中老年运动健康
            if(VariableConstants.STRING_CONSTANT_7.equals(classification)){
                equipmentNumber = quinquagenarianSportsHealthEquipmentMapper.selectCountAll();
                enableAmount = quinquagenarianSportsHealthEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //残疾人运动健康
            if(VariableConstants.STRING_CONSTANT_8.equals(classification)){
                equipmentNumber = deformitySportsHealthEquipmentMapper.selectCountAll();
                enableAmount = deformitySportsHealthEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //体育培训
            if(VariableConstants.STRING_CONSTANT_9.equals(classification)){
                equipmentNumber = sportsTrainingDetailsMapper.selectMonitoringCountSumByAll();
                enableAmount = sportsTrainingDetailsMapper.selectMonitoringCountSumByDeviceStatesOne();
            }
            //智慧步道
            if(VariableConstants.STRING_CONSTANT_10.equals(classification)){
                equipmentNumber = wisdomTrailsEquipmentMapper.selectCountAll();
                enableAmount = wisdomTrailsEquipmentMapper.selectCountByDeviceStatesOne();
            }
            //智慧场馆
            if(VariableConstants.STRING_CONSTANT_11.equals(classification)){
                equipmentNumber = wisdomVenuesEquipmentMapper.selectCountAll();
                enableAmount = wisdomVenuesEquipmentMapper.selectCountByDeviceStatesOne();
            }
            /**20200917： 产品要求修改意见：启用数量+运维监测数量=总数量，但是根本就是假数据，没有逻辑。。。为了好看，就适当更改*/
//            indexDeviceInfo.setEquipmentNumber(equipmentNumber).setEnableAmount(enableAmount);
            indexDeviceInfo.setEquipmentNumber(enableAmount+indexDeviceInfo.getAnomalyCount()).setEnableAmount(enableAmount);
        });
        List<WifeDevice> wifeDeviceList = wifeDeviceService.selectByType(1);
        IndexDeviceInfo info = getIndexDeviceInfo(wifeDeviceList, VariableConstants.STRING_CONSTANT_3);
        indexDeviceInfos.add(info);

        List<WifeDevice> wifeDevices = wifeDeviceService.selectByType(2);
        IndexDeviceInfo deviceInfo = getIndexDeviceInfo(wifeDevices, VariableConstants.STRING_CONSTANT_1);
        indexDeviceInfos.add(deviceInfo);
        return indexDeviceInfos;
    }

    private IndexDeviceInfo getIndexDeviceInfo(List<WifeDevice> wifeDeviceList, String type) {
        IndexDeviceInfo info = new IndexDeviceInfo();
        info.setClassification(type);
        info.setEquipmentNumber(wifeDeviceList.size());
        int enableAmount = 0;
        int anomalyCount = 0;
        for (WifeDevice device : wifeDeviceList) {
            if (device.getIsActive() == YesOrNo.YES) {
                enableAmount++;
            }
            if (device.getStatus() != 1) {
                anomalyCount++;
            }
        }
        info.setEnableAmount(enableAmount);
        info.setAnomalyCount(anomalyCount);
        return info;
    }

    /**
     * @方法名：deletetStreetDeviceClassifyByIds
     * @描述： 批量删除【首页-设备统计】
     * @作者： kjz
     * @日期： Created in 2020/8/11 18:04
     */
    @Transactional(rollbackFor = Exception.class)
    public int deletetStreetDeviceClassifyByIds(String ids) {
        if (ids == null || "".equals(ids)) {
            throw new CustomException(StatusCode.ID_IS_NULL);
        }
        String[] idArray = ids.split(",");

        return this.indexDeviceInfoMapper.deletetStreetDeviceClassifyByIds(idArray);
    }
}
